from smart_m3.m3_kp import *
import time
import sys
import uuid
import random

# Create a node instance
# One can also subclass ParticipantNode
# Constructor argument: String representing node id
# Node id can be anything, not restricted by the system





node = KP("SIB Tester")
# ss_handle = node.discover(method = "mDNS")
ss_handle = node.discover()
print ss_handle

if not node.join(ss_handle):
    sys.exit('Could not join to Smart Space')

print "--- Member of SS:", node.member_of


# Create a proactive (sending) session with the
# smart space


# Put information to smart space using the session
# created earlier
# Parameters: string containing the information
#             string containing the information type
# Information and type can be any string, they are
# not restricted by the system

def convert(triples):
    tl = []
    for t in triples:
        tr, stype, otype = t
        _s, _p, _o = tr
        if stype.lower() == "uri":
            s = URI(_s)
        elif stype.lower() == "literal":
            s = Literal(_s)
        else:
            s = bNode(_s)
        p = URI(_p)
        if otype.lower() == "uri":
            o = URI(_o)
        else:
            o = Literal(_o)
        tl.append(Triple(s, p, o))
    return tl



# Name space arces
ns = "http://mml.arces.unibo.it/Ontology1243431093.owl#"

ns_core = "http://www.sofia-project.eu/ontologies/core/2010/01/19/sofia.owl#"
ns_semint = "http://sofia.gotdns.com/ontologies/SemanticInteraction.owl#"
ns_sc = "http://sofia.gotdns.com/ontologies/SemanticConnections.owl#"
ns_sc_add = "http://sofia.gotdns.com/ontologies/SemanticConnections_UnBo.owl#"

# Class
Thing= ns + "Thing"
SmartObject = ns_core + "SmartObject"
MediaPlayer = ns_core + "MediaPlayer"
InteractionPrimitive = ns_semint + "InteractionPrimitive"
InteractionPrimitiveType = ns_semint + "InteractionPrimitiveType"
Resolution = ns_core + "Resolution"
MediaPlayerEvent = ns_core + "MediaPlayerEvent"
Event = ns_core + "Event"
Identification= ns_core + "Identification"
IDType = ns_semint + "IDType"

# Instances
SmartObject_01= ns_core + "SmartObject_" + str(uuid.uuid1())
MediaPlayer_01= ns_core + "MediaPlayer_" + str(uuid.uuid1())

Identification_01= ns_core + "Identification_" + str(uuid.uuid1())
Identification_02= ns_core + "Identification_" + str(uuid.uuid1())

InteractionPrimitive_01 = ns_semint + "InteractionPrimitive_" + str(uuid.uuid1())
InteractionPrimitive_02 = ns_semint + "InteractionPrimitive_" + str(uuid.uuid1())
InteractionPrimitive_03 = ns_semint + "InteractionPrimitive_" + str(uuid.uuid1())
InteractionPrimitive_04 = ns_semint + "InteractionPrimitive_" + str(uuid.uuid1())


MoveDown = ns_sc + "MoveDown"
MoveUp = ns_sc + "MoveUp"
MoveRight = ns_sc + "MoveRight"
MoveLeft = ns_sc + "MoveLeft"

PlayEvent = ns_sc + "PlayEvent"
StopEvent = ns_sc + "StopEvent"
NextEvent = ns_sc + "NextEvent"
PreviousEvent = ns_sc + "PreviousEvent"

Bynary = ns_semint + "Binary"
EPC_RFID = ns_semint + "EPC_RFID"


# Propriety
connectedTo= ns_sc +"connectedTo"
hasIdentification= ns_core+"hasIdentification"
ofIDType = ns_semint + "ofIDType"
idValue = ns_semint + "idValue"
hasInteractionPrimitive = ns_semint + "hasInteractionPrimitive"
hasInteractionPrimitiveType = ns_semint + "hasInteractionPrimitiveType"

hasResolution = ns_semint + "hasResolution"
dataValue = ns_semint + "dataValue"
canBeTransformedTo = ns_semint + "canBeTransformedTo"
launchesEvent = ns_core + "launchesEvent"
hasEventType = ns_sc_add + "hasEventType"
hasMediaPlayerEvent = ns_sc_add + "hasMediaPlayerEvent"


#definisce le classi come sottoclassi di Thing
_classes = 	[((SmartObject, "rdfs:subClassOf",Thing),"uri","uri"),
	 	((MediaPlayer,"rdfs:subClassOf",Thing), "uri", "uri"),
		((InteractionPrimitive, "rdfs:subClassOf", Thing),"uri","uri"),
		((Resolution, "rdfs:subClassOf", Thing), "uri","uri"),
		((MediaPlayerEvent,"rdfs:subClassOf",Thing), "uri", "uri"),
		((Event,"rdfs:subClassOf",Thing), "uri", "uri"),
		((Identification,"rdfs:subClassOf",Thing), "uri", "uri"),
		((IDType,"rdfs:subClassOf",Thing), "uri", "uri")]


#instanze delle classi definite sopra
_instances =	[#((SmartObject_01, "rdf:type", SmartObject), "uri", "uri"),
		#((MediaPlayer_01, "rdf:type", MediaPlayer), "uri", "uri"),
		#((Identification_01, "rdf:type", Identification),"uri","uri"),
		#((Identification_02, "rdf:type", Identification),"uri","uri"),

		
		#((InteractionPrimitive_01,"rdf:type",InteractionPrimitive), "uri","uri"),
		#((InteractionPrimitive_02,"rdf:type",InteractionPrimitive), "uri","uri"),
		#((InteractionPrimitive_03,"rdf:type",InteractionPrimitive), "uri","uri"),
		#((InteractionPrimitive_04,"rdf:type",InteractionPrimitive), "uri","uri"),

		((MoveUp,"rdf:type",InteractionPrimitiveType), "uri","uri"),
		((MoveDown,"rdf:type",InteractionPrimitiveType), "uri","uri"),
		((MoveRight,"rdf:type",InteractionPrimitiveType), "uri","uri"),
		((MoveLeft,"rdf:type",InteractionPrimitiveType), "uri","uri"),
                

		(( PlayEvent,"rdf:type",MediaPlayerEvent), "uri","uri"),
		(( StopEvent,"rdf:type",MediaPlayerEvent), "uri","uri"),
		(( NextEvent,"rdf:type",MediaPlayerEvent), "uri","uri"),
		(( PreviousEvent,"rdf:type",MediaPlayerEvent), "uri","uri"),
                
		
                #((Bynary,"rdf:type",Resolution), "uri","uri"),
                ((EPC_RFID,"rdf:type",IDType), "uri","uri")]


#attributi e proprieta'
_attributes =  [
		#((MediaPlayer_01, hasIdentification, Identification_01), "uri", "uri"),
		#((MediaPlayer_01, hasFrendlyName, "MediaPlayerCasa"), "uri", "literal"),
		#((Identification_01, idValue, "01"), "uri", "literal"),
		#((Identification_01, idValue, "E002220C4708B1FD"), "uri", "literal"),   
		#RFID test

		#((MediaPlayer_01, hasMediaPlayerEvent, PlayEvent),"uri", "uri"),
		#((MediaPlayer_01, hasMediaPlayerEvent, StopEvent),"uri", "uri"),
		#((MediaPlayer_01, hasMediaPlayerEvent, NextEvent),"uri", "uri"),
		#((MediaPlayer_01, hasMediaPlayerEvent, PreviousEvent),"uri", "uri"),

		#((InteractionPrimitive_01, canBeTransformedTo , PlayEvent),"uri", "uri"),
		#((InteractionPrimitive_02, canBeTransformedTo , StopEvent),"uri", "uri"),
		#((InteractionPrimitive_03, canBeTransformedTo , NextEvent),"uri", "uri"),
		#((InteractionPrimitive_04, canBeTransformedTo , PreviousEvent),"uri", "uri"),

		#((InteractionPrimitive_01, hasInteractionPrimitiveType , MoveUp),"uri", "uri"),
		#((InteractionPrimitive_02, hasInteractionPrimitiveType , MoveDown),"uri", "uri"),
		#((InteractionPrimitive_03, hasInteractionPrimitiveType , MoveRight),"uri", "uri"),
		#((InteractionPrimitive_04, hasInteractionPrimitiveType , MoveLeft),"uri", "uri"),
                
		#((SmartObject_01, hasInteractionPrimitive,InteractionPrimitive_01),"uri","uri"),
		#((SmartObject_01, hasInteractionPrimitive,InteractionPrimitive_02),"uri","uri"),
		#((SmartObject_01, hasInteractionPrimitive,InteractionPrimitive_03),"uri","uri"),
		#((SmartObject_01, hasInteractionPrimitive,InteractionPrimitive_04),"uri","uri")
		]


classes = convert(_classes)
instances = convert(_instances)
#attributes = convert(_attributes)

print "BASIC_TEST_ARCES_QUERY running..."
print ""
print "Inserting graph..."
pro = node.CreateInsertTransaction(ss_handle)
try:
    pro.send(classes, confirm = True)
except M3Exception:
    print "Insert (class) failed:", M3Exception

try:
    pro.send(instances, confirm = True)
except M3Exception:
    print "Insert (instances) failed:", M3Exception

#try:
#    pro.send(attributes, confirm = True)
#except M3Exception:
#    print "Insert (attributes) failed:", M3Exception



node.CloseInsertTransaction(pro)
print "Done."
print ""






time.sleep(3)

node.leave(ss_handle)
print "Left smart space"
